/*
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.anime.logic;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cc.aileron.anime.ApplicationConfig;

import com.google.inject.Inject;

/**
 * @author Aileron
 */
public class PreviewMovieImpl implements PreviewMovie
{
    @Override
    public boolean playing()
    {
        return stop == false;
    }

    @Override
    public void run()
    {
        doShow();
    }

    @Override
    public void stop()
    {
        stop = true;
    }

    /**
     * @throws InterruptedException
     * 
     */
    private void doShow()
    {
        mainFrame.getContentPane()
            .removeAll();

        final int size = snapshot.count();
        int i;

        stop = false;
        i = 0;
        while (i < size)
        {
            mainFrame.getContentPane()
                .removeAll();

            final String fileName = targetImageDir + i + ".jpeg";
            final ImageIcon icon = new ImageIcon(fileName);
            final JLabel label = new JLabel(icon);
            final JPanel panel = new JPanel();
            panel.add(label);
            mainFrame.getContentPane()
                .add(panel);
            mainFrame.pack();

            logger.debug("output: {}", fileName);

            try
            {
                Thread.sleep(sleepTime);
            }
            catch (final InterruptedException e)
            {
            }

            /*
             * stop 要求が有る場合は、停止
             */
            if (stop)
            {
                break;
            }

            /*
             * stop 要求が無い場合は、リプレイ
             */
            if (i + 1 == size && stop == false)
            {
                mainFrame.getContentPane()
                    .removeAll();
                i = 0;
                continue;
            }

            /*
             * 次へ
             */
            i += 1;
        }
    }

    /**
     * @param mainFrame
     * @param snapshot
     * @param config
     */
    @Inject
    public PreviewMovieImpl(
        final JFrame mainFrame,
        final SnapshotStatus snapshot,
        final ApplicationConfig config)
    {
        this.mainFrame = mainFrame;
        this.snapshot = snapshot;
        this.sleepTime = config.sleepTime;
        this.targetImageDir = config.targetImageDir;
    }

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final JFrame mainFrame;
    private final int sleepTime;
    private final SnapshotStatus snapshot;
    private volatile boolean stop = true;
    private final String targetImageDir;
}
